﻿using System;
using System.Text.RegularExpressions;
using DataProcess.Interface;
using DataProcess.Interface.Models;

namespace DataProcessing.BrukerXRF
{
    public class DataProcessing : IDataProcessing
    {
        public string Name { get { return "Bruker(布鲁克) 荧光仪"; } }

        public IDataOutput Processing(string content)
        {
            XRayOutputModel outputModel = new XRayOutputModel();
            var regContext = new Regex("[\r\n]+").Replace(content, "\r");
            string[] spdata = regContext.Split('\r');
            outputModel.AnalyzeApplicationName = spdata[0];
            outputModel.SampleName = spdata[1];
            var dateIndex = 0;
            foreach (var item in spdata)
            {
                dateIndex++;
                if (item.Equals("END"))
                {
                    break;
                }
                int emp = item.IndexOf(" ");
                int tab = item.IndexOf("\t");
                if (emp > -1 || tab > -1)
                {
                    var reItem = new Regex("[\\s]+").Replace(item, " ");
                    string[] spItem = reItem.Split(' ');
                    string key = spItem[0].TrimStart().TrimEnd();

                    if (!outputModel.Eelements.ContainsKey(key))
                    {
                        outputModel.Eelements.Add(key, spItem[4]);
                    }
                }
            }

            if (DateTime.TryParse(spdata[dateIndex], out DateTime date))
            {
                outputModel.MeasureTime = date;
            }
            else
            {
                outputModel.MeasureTime = DateTime.Now;
            }
            return outputModel;
        }
    }
}
